package com.lanou.myapp.comic.rankingcomic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lanou.myapp.R;
import com.lanou.myapp.comic.rankingcomic.contentrankingcomic.ContentrankComicActivity;
import com.lanou.myapp.greendao.RankingComic;
import com.lanou.myapp.help.RankingComicDaoHelper;
import com.lanou.myapp.help.DaoSingleton;
import com.lanou.myapp.progressdialog.Dialog;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by dllo on 16/1/11.
 */
public class RankingComicFragment extends Fragment{
    private Context context;
    private ListView listView;
    private RankingComicAdapter adapter;
//    private String url = "http://v2.api.dmzj.com/latest/100/0.json";
    private String url = "http://v2.api.dmzj.com/rank/0/0/0/0.json";
    private RequestQueue queue;
    private List<RankingComic> rankingComics;
    private Dialog dialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView) getActivity().findViewById(R.id.listView_ranking_comic);
        dialog = new Dialog(context,"dfdfd");
        dialog.setTitle("正在加载，请稍后...");
        dialog.show();
        queue = Volley.newRequestQueue(getActivity());
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Type type = new TypeToken<List<com.lanou.myapp.greendao.RankingComic>>(){}.getType();
                rankingComics = new ArrayList<>();
                rankingComics = gson.fromJson(response,type);

                RankingComicDaoHelper.getInstance().addAll(rankingComics);

                adapter = new RankingComicAdapter(rankingComics,getActivity());
                listView.setAdapter(adapter);
                dialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                adapter = new RankingComicAdapter(RankingComicDaoHelper.getInstance().queryAll(),getActivity());
            }
        });
        queue.add(request);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ContentrankComicActivity.class);
                intent.putExtra("key", rankingComics.get(position).getComic_id());
                getActivity().startActivity(intent);
            }
        });


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_ranking_comic,null);
        return view;
    }

}
